package com.example.textviewactivity.Adapter;

import android.content.Context;
import android.media.Image;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.textviewactivity.R;
import com.example.textviewactivity.pojo.Account;

import java.util.List;

public class ListViewAdapter extends BaseAdapter {
    List<Account> accounts;
    Context context;

    public ListViewAdapter(List<Account> accounts, Context context) {
        this.accounts = accounts;
        this.context = context;
    }

    @Override
    public int getCount() {
        return accounts != null ? accounts.size() : 0;
    }

    @Override
    public Object getItem(int i) {
        return accounts.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        if (view == null) {
            view = LayoutInflater.from(context).inflate(R.layout.listviewlayout, null);
        }
        ImageView imageView;
        TextView acc_title, acc_intro, acc_price;

        Account account = accounts.get(i);
        imageView = view.findViewById(R.id.acc_image);
        acc_title=view.findViewById(R.id.acc_title);
        acc_intro=view.findViewById(R.id.acc_intro);
        acc_price=view.findViewById(R.id.acc_price);

        imageView.setImageResource(account.getImageId());
        acc_title.setText(account.getTitle());
        acc_intro.setText(account.getIntro());
        acc_price.setText(account.getPrice()+"");
        return view;
    }
}
